package com.aboo.action;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import net.sf.json.JSONArray;

import org.apache.commons.lang3.StringUtils;

import com.aboo.po.MenuPO;
import com.aboo.po.Option;
import com.aboo.po.Page;
import com.aboo.po.RolePO;
import com.aboo.pub.constant.Constant;
import com.aboo.pub.exception.BasicException;
import com.aboo.service.RoleService;
import com.aboo.util.AjaxUtil;
import com.aboo.util.RequestUtil;

public class RoleAction extends BaseAction {
	
	private static final long serialVersionUID = 4830568821323476876L;
	private RoleService roleService;
	private RolePO role = new RolePO();
	
	@SuppressWarnings("unchecked")
	public String list() throws BasicException {
		Integer currentPageNO = RequestUtil.getParameterIntegerDefault(request, Page.CURRENT_PAGE_NO, Page.DEFAULT_CURRENT_PAGE_NO);
		Integer pageSize = RequestUtil.getParameterIntegerDefault(request, Page.PAGE_SIZE, Page.DEFAULT_PAGE_SIZE);
		Integer sectionSize = RequestUtil.getParameterIntegerDefault(request, Page.SECTION_SIZE, Page.DEFAULT_SECTION_SIZE);
		
		page = new Page(currentPageNO, pageSize, sectionSize);
		String tmpRoleName = null;
		if (StringUtils.isNotEmpty(role.getName())) {
			tmpRoleName = role.getName();
			role.addCondition("NAME", Constant.CONDITION_LIKE, "%" + tmpRoleName + "%");
			role.setName("");
		}
		roleService.findRoleList(ri, role, page);
		List<RolePO> result = (List<RolePO>) ri.getObj();
		
		if (StringUtils.isNotEmpty(tmpRoleName)) {
			role.setName(tmpRoleName);
		}
		RequestUtil.setAttribute(request, "roleList", result);
		return BaseAction.LIST;
	}
	
	public String create() throws BasicException {
		return BaseAction.CREATE;
	}
	
	public String createExc() throws BasicException {
		if (StringUtils.isNotEmpty(role.getName())) {
			RolePO param = new RolePO();
			param.setName(role.getName());
			roleService.findRoleCount(ri, param);
			Integer count = (Integer)ri.getObj();
			if (count != null && count.intValue() > 0) {
				throw new BasicException("存在相同名称的角色，无法创建！");
			}
		}
		roleService.saveRole(ri, role);
		return BaseAction.CREATE_EXC;
	}
	
	public String delete() throws BasicException {
		String checkedIds = RequestUtil.getParameterStringDefault(request, "checkedIds", "");
		if (!StringUtils.isEmpty(checkedIds)) {
			String[] checkedIdAtt = checkedIds.split(",");
			List<String> checkedIdList = Arrays.asList(checkedIdAtt);
			roleService.deleteRole(ri, checkedIdList);
		}		
		
		return BaseAction.DELETE;
	}
	
	public String modify() throws BasicException {
		String checkedId = RequestUtil.getParameterStringDefault(request, "checkedIds", "");
		role.setId(new Integer(checkedId));
		roleService.findRole(ri, role);
		role = (RolePO) ri.getObj();
		return BaseAction.MODIFY;
	}
	
	public String view() throws BasicException {
		String checkedId = RequestUtil.getParameterStringDefault(request, "checkedIds", "");
		role.setId(new Integer(checkedId));
		roleService.findRole(ri, role);
		role = (RolePO) ri.getObj();
		return BaseAction.VIEW;
	}
	
	public String modifyExc() throws BasicException {
		roleService.updateRole(ri, role);
		return BaseAction.MODIFY_EXC;
	}
	
	public String treeMenu() throws BasicException {
		String roleId = RequestUtil.getParameterStringDefault(request, "roleId", "");
		RequestUtil.setAttribute(request, "roleId", roleId);
		return "treeMenu";
	}
	
	public String treeMenuExc() throws BasicException {
		String roleId = RequestUtil.getParameterStringDefault(request, "roleId", "");
		String checkedIds = RequestUtil.getParameterStringDefault(request, "checkedIds", "");
		if (!StringUtils.isEmpty(roleId)) {
			String[] checkedIdArr = new String[0];
			if (!StringUtils.isEmpty(checkedIds)) {
				checkedIdArr = checkedIds.split(",");
			}
			
			roleService.saveRoleMenuList(ri, new Integer(roleId), Arrays.asList(checkedIdArr));
		}
		return "treeMenuExc";
	}
	
	@SuppressWarnings("unchecked")
	public String getAllSelectedMenu() throws BasicException {
		String roleId = RequestUtil.getParameterStringDefault(request, "roleId", "");
		roleService.findRoleMenuList(ri, new Integer(roleId));
		List<MenuPO> resourceList = (List<MenuPO>) ri.getObj();
		
		String jsonStr = new String("");
		if (resourceList != null) {
			List<Option> ops = new ArrayList<Option>();
			for (MenuPO res : resourceList) {
				ops.add(new Option(res.getId().toString(), res.getName(), res.getParentId().toString()));
			}
			JSONArray ja = JSONArray.fromObject(ops);
			jsonStr = ja.toString();
		}
		
		AjaxUtil.doAjax(response, jsonStr);
		return null;
	}
	
	public RoleService getRoleService() {
		return roleService;
	}
	public void setRoleService(RoleService roleService) {
		this.roleService = roleService;
	}
	public RolePO getRole() {
		return role;
	}
	public void setRole(RolePO role) {
		this.role = role;
	}

}
